//
//  OnsiteContext.m
//  MySpaceID
//
//  Created by Haider Sabri on Thursday6/11/09.
//  Copyright 2009 MySpace, Inc. All rights reserved.
//

#import "OnsiteContext.h"
#import "OAConsumer.h"
#import "OAMutableURLRequest.h"
#import "OADataFetcher.h"


@implementation OnsiteContext

+ (OnsiteContext*) contextWithConsumerKey:(NSString*) consumerKey
						 consumerSecret:(NSString*) consumerSecret
{
	OnsiteContext *context = [[[OnsiteContext alloc] init] autorelease];
	context.consumerKey = consumerKey;
	context.consumerSecret = consumerSecret;
	context.urlScheme = @"myspaceid";
	return context;
}
-(id) init
{
	if (self = [super init])
	{
		request= nil;
		consumer = nil;
	}
	
	return self;
}
- (void) dealloc{
	[consumer release];
	[request release];
	[super dealloc];
}

- (void) makeRequest:(NSURL*)url:(NSString*)method: (NSString*) body: (id<OAResponseDelegate>)delegate
{	
	if (consumer)
	{
		[consumer release];
		consumer = nil;
	}
	
	if (request)
	{
		[request release];
		request = nil;
	}
	
	consumer = [[OAConsumer alloc] initWithKey:consumerKey secret:consumerSecret];
	request = [[OAMutableURLRequest alloc] initWithURL:url
									consumer:consumer
									token:nil   
									realm:nil];  // our service provider doesn't specify a realm
		
	[request setHTTPMethod:method];
	if(body)
	{
		NSData *bodyData = [body dataUsingEncoding:NSASCIIStringEncoding];
		[request setHTTPBody:bodyData];
		[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
		
	} 
	OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease];
	[fetcher fetchDataWithRequest:request
							 delegate:delegate
					didFinishSelector:@selector(apiTicket:didFinishWithData:)
					  didFailSelector:@selector(apiTicket:didFinishWithError:)];

}


@end
